// 存储配置的键名常量
const STORAGE_KEYS = {
  TENCENT: {
    ENABLED: 'tencentEnabled',
    SECRET_ID: 'tencentSecretId',
    SECRET_KEY: 'tencentSecretKey'
  },
  CHATGLM: {
    ENABLED: 'chatglmEnabled',
    API_KEY: 'chatglmApiKey',
    API_SECRET: 'chatglmApiSecret',
    ASSISTANT_ID: 'assistantId'
  }
};

// 显示提示消息
function showToast(message) {
  const toast = document.createElement('div');
  toast.className = 'toast';
  toast.textContent = message;
  document.body.appendChild(toast);
  
  setTimeout(() => toast.classList.add('show'), 10);
  setTimeout(() => {
    toast.classList.remove('show');
    setTimeout(() => document.body.removeChild(toast), 300);
  }, 3000);
}

// 重构开关状态控制函数
function toggleSwitch(element, enabled) {
  if (enabled) {
    element.classList.add('active');
  } else {
    element.classList.remove('active');
  }
}

// 密码显示控制函数
function togglePasswordVisibility(element) {
  // 获取当前开关状态
  const isPasswordVisible = element.classList.contains('active');
  
  // 获取所有密码输入框
  const passwordInputs = document.querySelectorAll('input[type="password"], input[type="text"].password-field');
  
  // 切换密码显示状态
  passwordInputs.forEach(input => {
    // 如果当前是显示状态，就改为隐藏
    if (isPasswordVisible) {
      input.type = 'password';
      element.classList.remove('active');
    } 
    // 如果当前是隐藏状态，就改为显示
    else {
      input.type = 'text';
      // 添加一个类来标记这是密码字段
      input.classList.add('password-field');
      element.classList.add('active');
    }
  });
}

// 保存设置
async function saveSettings() {
  const settings = {
    [STORAGE_KEYS.TENCENT.ENABLED]: document.getElementById('tencentSwitch').classList.contains('active'),
    [STORAGE_KEYS.TENCENT.SECRET_ID]: document.getElementById('tencentSecretId').value,
    [STORAGE_KEYS.TENCENT.SECRET_KEY]: document.getElementById('tencentSecretKey').value,
    [STORAGE_KEYS.CHATGLM.ENABLED]: document.getElementById('chatglmSwitch').classList.contains('active'),
    [STORAGE_KEYS.CHATGLM.API_KEY]: document.getElementById('chatglmApiKey').value,
    [STORAGE_KEYS.CHATGLM.API_SECRET]: document.getElementById('chatglmApiSecret').value,
    [STORAGE_KEYS.CHATGLM.ASSISTANT_ID]: document.getElementById('assistantId').value
  };

  try {
    await chrome.storage.sync.set(settings);
    showToast('设置已保存！');
  } catch (error) {
    showToast('保存设置失败！');
    console.error('保存设置失败:', error);
  }
}

// 加载设置
async function loadSettings() {
  try {
    const settings = await chrome.storage.sync.get(Object.values(STORAGE_KEYS.TENCENT).concat(Object.values(STORAGE_KEYS.CHATGLM)));
    
    // 腾讯云设置
    document.getElementById('tencentSecretId').value = settings[STORAGE_KEYS.TENCENT.SECRET_ID] || '';
    document.getElementById('tencentSecretKey').value = settings[STORAGE_KEYS.TENCENT.SECRET_KEY] || '';
    toggleSwitch(document.getElementById('tencentSwitch'), settings[STORAGE_KEYS.TENCENT.ENABLED]);
    
    // 智谱轻言设置
    document.getElementById('chatglmApiKey').value = settings[STORAGE_KEYS.CHATGLM.API_KEY] || '';
    document.getElementById('chatglmApiSecret').value = settings[STORAGE_KEYS.CHATGLM.API_SECRET] || '';
    document.getElementById('assistantId').value = settings[STORAGE_KEYS.CHATGLM.ASSISTANT_ID] || '';
    toggleSwitch(document.getElementById('chatglmSwitch'), settings[STORAGE_KEYS.CHATGLM.ENABLED]);

    // 确保所有密码字段初始状态为隐藏
    const togglePasswordBtn = document.getElementById('togglePassword');
    togglePasswordBtn.classList.remove('active');
    document.querySelectorAll('input[type="text"].password-field').forEach(input => {
      input.type = 'password';
      input.classList.remove('password-field');
    });
  } catch (error) {
    console.error('加载设置失败:', error);
    showToast('加载设置失败！');
  }
}

// 初始化事件监听
function initializeEventListeners() {
  // 密码显示切换
  const togglePasswordBtn = document.getElementById('togglePassword');
  togglePasswordBtn.addEventListener('click', function() {
    togglePasswordVisibility(this);
  });

  // 服务开关切换
  const serviceToggles = {
    tencentSwitch: '腾讯云翻译',
    chatglmSwitch: '智谱轻言'
  };

  Object.keys(serviceToggles).forEach(id => {
    const toggle = document.getElementById(id);
    toggle.addEventListener('click', function() {
      const newState = !this.classList.contains('active');
      toggleSwitch(this, newState);
      showToast(`${serviceToggles[id]}服务已${newState ? '启用' : '禁用'}`);
    });
  });

  // 保存设置
  document.getElementById('saveSettings').addEventListener('click', saveSettings);
}

// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', () => {
  loadSettings();
  initializeEventListeners();
}); 